package ru.sushka.lunch.dao.psql;

import java.sql.ResultSet;
import java.sql.SQLException;

import ru.sushka.lunch.enitiy.Course;

public class PsqlCourseDao extends PsqlDao<Course>{

    private final String tableName = "courses";
    
    @Override
    protected String getTableName() {
        return tableName;
    }

    @Override
    protected String getSelectSqlQuery(int id) {
        return "select * from " + tableName + " where id = " + id;
    }

    @Override
    protected String getDeleteSqlQuery(int id) {
        return "delete from " + tableName + " where id = " + id;
    }

    @Override
    protected String getInsertSqlQuery(Course persistentce) {
        return "insert into " + tableName + "(name, price, category, countable, consist) values('" + 
                                persistentce.getName() + "'," + 
                                persistentce.getPrice() + "," +
                                persistentce.getCategory() + "," +
                                persistentce.isCountable() + ",'"+
                                persistentce.getConsist() + "')";
    }

    @Override
    protected String getUpdateSqlQuery(Course persistentce) {
        return "update customers set id=" + persistentce.getId() + "," + 
                                        "name = '"+persistentce.getName()+"',"+
                                        "price = "+persistentce.getPrice()+","+
                                        "category = "+persistentce.getCategory()+","+
                                        "countable = "+persistentce.isCountable()+","+
                                        "consist = '"+persistentce.getConsist()+"',"+
                                        "where id=" + persistentce.getId();
    }

    @Override
    protected Course parseResultSet(ResultSet rs) throws SQLException{
        int id = rs.getInt(1);
        String name = rs.getString(2);
        Double price = rs.getDouble(3);
        int category = rs.getInt(4);
        boolean countable = rs.getBoolean(5);
        String consist = rs.getString(6);
        Course course = new Course(name, price, category, consist, countable);
        course.setId(id);
        return course;
    }

}
